package io.renren.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.modules.pms.entity.PmsProductCategoryEntity;
import io.renren.modules.pms.entity.ProductAttributeCategoryEntity;
import io.renren.modules.pms.entity.ProductCategoryAttributeRelationEntity;
import io.renren.modules.pms.entity.dto.RelationAttrInfoByCidDTO;
import io.renren.modules.pms.service.ProductAttributeCategoryService;
import io.renren.modules.pms.service.ProductCategoryAttributeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.pms.dao.ProductAttributeDao;
import io.renren.modules.pms.entity.ProductAttributeEntity;
import io.renren.modules.pms.service.ProductAttributeService;

import javax.annotation.Resource;


@Service("productAttributeService")
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeDao, ProductAttributeEntity> implements ProductAttributeService {

    @Resource
    ProductAttributeDao productAttributeDao;
    @Autowired
    ProductCategoryAttributeRelationService productCategoryAttributeRelationService;

    @Autowired
    ProductAttributeCategoryService productAttributeCategoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttributeEntity> page = this.page(
                new Query<ProductAttributeEntity>().getPage(params),
                new QueryWrapper<ProductAttributeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params,long cid) {
        IPage<ProductAttributeEntity> page = this.page(
                new Query<ProductAttributeEntity>().getPage(params),
                new QueryWrapper<ProductAttributeEntity>()
                        .lambda()
                        .eq(ProductAttributeEntity::getProductAttributeCategoryId,cid)
                        .eq(ProductAttributeEntity::getType,params.get("type"))
        );

        return new PageUtils(page);
    }

    @Override
    public boolean creat(ProductAttributeEntity productAttributeEntity) {
        //新增商品属性
        boolean save = this.save(productAttributeEntity);

        //新增商品属性以后需要更新商品属性分类数量
        if (save){
            QueryWrapper<ProductAttributeEntity> queryWrapper=new QueryWrapper();
            UpdateWrapper<ProductAttributeCategoryEntity> updateWrapper = new UpdateWrapper<>();
            //更新属性数量
            if (productAttributeEntity.getType() == 0){

                int count = this.count(queryWrapper
                        .eq("product_attribute_category_id",productAttributeEntity.getProductAttributeCategoryId())
                        .eq("type",0));
                updateWrapper.lambda()
                        .set(ProductAttributeCategoryEntity::getAttributeCount,count)
                        .eq(ProductAttributeCategoryEntity::getId,productAttributeEntity.getProductAttributeCategoryId());
                productAttributeCategoryService.update(updateWrapper);

                return true;

                //更新参数数量
            }else if (productAttributeEntity.getType() == 1){

                int count = this.count(queryWrapper
                        .eq("product_attribute_category_id",productAttributeEntity.getProductAttributeCategoryId())
                        .eq("type",1));
                updateWrapper.lambda()
                        .set(ProductAttributeCategoryEntity::getParamCount,count)
                        .eq(ProductAttributeCategoryEntity::getId,productAttributeEntity.getProductAttributeCategoryId());
                productAttributeCategoryService.update(updateWrapper);

                return true;
            }

        }

        return false;
    }

    @Override
    public boolean delete(Map<String, Object> map) {
        //删除属性
        List<Long[]>  ids = (List<Long[]>) map.get("ids");
        ProductAttributeEntity productAttributeEntity = this.getById(ids.get(0));
        boolean save = this.removeByIds(ids);

        //删除商品属性以后需要更新属性数量
        if (save){

            QueryWrapper<ProductAttributeEntity> queryWrapper=new QueryWrapper();
            UpdateWrapper<ProductAttributeCategoryEntity> updateWrapper = new UpdateWrapper<>();

            if (productAttributeEntity.getType() == 0){

                int count = this.count(queryWrapper
                        .eq("product_attribute_category_id",productAttributeEntity.getProductAttributeCategoryId())
                        .eq("type",0));
                updateWrapper.lambda()
                        .set(ProductAttributeCategoryEntity::getAttributeCount,count)
                        .eq(ProductAttributeCategoryEntity::getId,productAttributeEntity.getProductAttributeCategoryId());
                productAttributeCategoryService.update(updateWrapper);

                return true;

                //更新参数数量
            }else if (productAttributeEntity.getType() == 1){

                int count = this.count(queryWrapper
                        .eq("product_attribute_category_id",productAttributeEntity.getProductAttributeCategoryId())
                        .eq("type",1));
                updateWrapper.lambda()
                        .set(ProductAttributeCategoryEntity::getParamCount,count)
                        .eq(ProductAttributeCategoryEntity::getId,productAttributeEntity.getProductAttributeCategoryId());
                productAttributeCategoryService.update(updateWrapper);

                return true;
            }
        }

        return false;
    }


    @Override
    public List<RelationAttrInfoByCidDTO> getRelationAttrInfoByCid(Long productCategoryId) {
        return productAttributeDao.getRelationAttrInfoByCid(productCategoryId);
    }

}
